import { Injectable, HttpException, HttpStatus } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { User } from './users.entity'

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User) private userRepository: Repository<User>,
  ) {}

  async created(user): Promise<User | undefined> {
    const { tel } = user
    const existTel = await this.userRepository.findOne({
      where: { tel },
    })
    if (existTel) {
      throw new HttpException('用户名已存在', HttpStatus.BAD_REQUEST)
    } else {
      return await this.userRepository.save(user)
    }
  }

  async findOne(id: string): Promise<string | any> {
    // return this.user.findByIds([id]);
    return `${id}}`
  }
}
